package com.ftf.tp.blog.infrastructure.repository;

import com.alibaba.fastjson.JSON;
import com.ftf.tp.blog.domain.article.aggregate.ArticleAgg;
import com.ftf.tp.blog.domain.configuration.dos.PageTagDO;
import com.ftf.tp.blog.application.vo.UserInfoVO;
import com.ftf.tp.blog.domain.configuration.dos.TagDO;
import com.ftf.tp.blog.domain.configuration.repository.TagRepository;
import com.ftf.tp.blog.infrastructure.converter.TagConverter;
import com.ftf.tp.blog.infrastructure.mapper.ArticleTagMapper;
import com.ftf.tp.blog.infrastructure.mapper.TagMapper;
import com.ftf.tp.blog.infrastructure.mapper.pos.ArticleTag;
import com.ftf.tp.blog.infrastructure.mapper.pos.TagPO;
import com.ftf.tp.common.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tanghao
 * @date 2022/7/28
 */
@Slf4j
@Repository
public class TagRepositoryImpl implements TagRepository {

    /**
     * 标签数据库mapper
     */
    @Resource
    private TagMapper tagMapper;

    @Resource
    private ArticleTagMapper articleTagMapper;

    /**
     * 获取所有的标签
     *
     * @param pageTagDO
     * @return 标签列表
     */
    @Override
    public List<TagDO> pageTag(PageTagDO pageTagDO) {
//        SearchRequest request = new SearchRequest("tag");
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//
//        if (ObjectUtils.isEmpty(pageTagDO.getTagName())) {
//            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//        } else {
//            searchSourceBuilder.query(QueryBuilders.matchQuery("tag_name", pageTagDO.getTagName()));
//        }
//        searchSourceBuilder.from((pageTagDO.getPage() - 1) * pageTagDO.getSize());
//        searchSourceBuilder.size(pageTagDO.getSize());
//        request.source(searchSourceBuilder);
//        List<TagEsPO> tagEsPOList = new ArrayList<>();
//        try {
//            SearchResponse search = client.search(request, RequestOptions.DEFAULT);
//            for (SearchHit hit : search.getHits()) {
//                Map<String, Object> map = hit.getSourceAsMap();
//                String id = hit.getId();
//                TagEsPO item = new TagEsPO();
//                item.setTagName((String) map.get("tag_name"));
//                item.setId(Long.valueOf(id));
//                tagEsPOList.add(item);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return TagConverter.INSTANCE.esToDOList(tagEsPOList);
        return null;
    }

    /**
     * 用户自定义标签
     *
     * @param tagDO 标签实体类
     */
    @Override
    public void addTagByUser(TagDO tagDO) {
        //判断用户是否达到要求等级
//        Object object = userFeign.getUserInfo();
        UserInfoVO userInfo = JSON.parseObject(JSON.toJSONString("object"), UserInfoVO.class);
        if (userInfo.getLevel() < 3) {
            throw new BusinessException("用户等级不足三级,无法创建自定义标签");
        }
        TagPO tagPO = TagConverter.INSTANCE.doToPO(tagDO);
        tagPO.setTagStatus(0);
        //标签类型 用户自定义标签
        tagPO.setTagType(1);
        tagPO.setFollowNumber(0);
        tagPO.setCreatedBy(userInfo.getId());
        tagMapper.insert(tagPO);
        log.info("用户:{} 成功创建自定义标签{}", userInfo.getId(), tagPO.getTagName());
    }

    /**
     * 获取所有大类标签
     *
     * @return 所有的大类标签
     */
    @Override
    public List<TagDO> getBigTagAll() {
        return TagConverter.INSTANCE.poToDOList(tagMapper.getBigTagAll());
    }

    /**
     * 获取标签列表
     *
     * @param tagIdList 标签idlist
     * @return 标签列表
     */
    @Override
    public List<TagDO> getTagByIdList(List<Long> tagIdList) {
        return TagConverter.INSTANCE.poToDOList(tagMapper.getTagByIdList(tagIdList));
    }

    /**
     * 文章标签关系同步
     *
     * @param articleAgg 文章聚合
     */
    @Override
    public void articleSync(ArticleAgg articleAgg) {
        articleTagMapper.batchInsert(articleAgg.getTagValues().stream().map(tagValue -> ArticleTag.builder()
                .articleId(articleAgg.getId())
                .tagId(tagValue.getId())
                .tagName(tagValue.getTagName())
                .build()).collect(Collectors.toList()));
    }
}
